<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ totalCounts.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ totalCounts.praisePercent }}</span
          ><span>好评率</span>
        </p>
        <p>
          <span>{{ totalCounts.evaluateCount }}</span
          ><span>条评价</span>
        </p>
        <p>
          <span>{{ totalCounts.hasPictureCount }}</span>
          <span>条有图评价</span>
        </p>
      </div>
    </div>

    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a
        @click="sortList('null')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === null }"
        >默认</a
      >
      <a
        @click="sortList('praiseCount')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'praiseCount' }"
        >最热</a
      >
      <a
        @click="sortList('createTime')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'createTime' }"
        >最新</a
      >
      <XtxCheckBox v-model="reqParams.hasPicture" style="margin-left: 50px"
        >是否有图</XtxCheckBox
      >
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in commentsList" :key="item.id">
        <!-- 用户 -->
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ item.member.nickname }}</span>
        </div>
        <!-- 用户发表内容 -->
        <div class="body">
          <!-- 商品星级 -->
          <div class="score">
            <!-- 实心星 -->
            <i
              v-for="(star, i) in item.score"
              :key="i"
              class="iconfont icon-wjx01"
            ></i>
            <!-- 空心星 -->
            <i
              v-for="(star, i) in 5 - item.score"
              :key="i"
              class="iconfont icon-wjx02"
            ></i>
            <span
              class="attr"
              v-for="(item, i) in item.orderInfo.specs"
              :key="i"
              >{{ item.name }} {{ item.nameValue }}</span
            >
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 使用图片预览组件 -->
          <GoodsCommentImage :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import GoodsCommentImage from './GoodsCommentImage'

// 获取统计信息
const useTotal = (route) => {
  const totalCounts = ref({})
  // 如果定义为null,会报错null,要在template渲染DOM的部分加v-if,保证代码健壮性，鲁棒性
  // 如果定义为空对象{},渲染数据只取一层不报错，取多层又会报错null，要在template渲染DOM的部分加v-if,保证代码健壮性，鲁棒性（robust）
  // const route = useRoute()
  const getTotalCounts = async () => {
    const { result } = await findCommentInfoByGoods(route.params.id)
    totalCounts.value = result
    // console.log(totalCounts.value)
  }

  onMounted(() => {
    getTotalCounts()
  })

  return {
    totalCounts
  }
}

// 获取评价列表
const useCommentsList = (route) => {
  const commentsList = ref({})
  //   const route = useRoute()

  const reqParams = ref({
    page: 1,
    pageSize: 10,
    hasPicture: false, // 是否有图
    sortField: null // 排序:默认：null ->最热: praiseCount -> 最新: createTime
  })

  const getCommentsList = async () => {
    // 解构对象里的对象
    const {
      result: { items }
    } = await findCommentListByGoods(route.params.id, reqParams.value)
    commentsList.value = items
    // console.log(commentsList.value)
  }

  onMounted(() => {
    getCommentsList()
  })

  return {
    commentsList,
    reqParams,
    getCommentsList
  }
}

export default {
  name: 'GoodsComment',

  components: { GoodsCommentImage },

  setup () {
    // route优化，引用一次再传参
    const route = useRoute()

    const { totalCounts } = useTotal(route)

    const { commentsList, reqParams, getCommentsList } = useCommentsList(route)

    // 这两个排序也可以写在 useCommentsList 里
    // 筛选排序
    const sortList = (sort) => {
      reqParams.value.sortField = sort
      console.log(reqParams.value.sortField)

      // 重新请求
      getCommentsList()
      console.log(commentsList)
    }

    // 是否有图的排序
    watch(
      () => reqParams.value.hasPicture,
      () => {
        getCommentsList()
      }
    )

    return { totalCounts, commentsList, reqParams, sortList }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex: 1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
